package com.gxuwu.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxuwu.admin.entity.*;
import com.gxuwu.admin.mapper.MyUserMapper;
import com.gxuwu.admin.service.MyUserService;
import com.gxuwu.admin.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class MyUserServiceImpl implements MyUserService {
    @Autowired
    private MyUserMapper myUserMapper;

    @Resource
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public PageInfo<MyUser> getUserList(Integer page,Integer pageSize,String username) {

         PageHelper.startPage(page,pageSize);

         Integer begin=(page-1)*pageSize;
        List<MyUser> myUsers = myUserMapper.selectByLimit(begin,pageSize,username);

        PageInfo<MyUser> pageInfo = new PageInfo<>(myUsers);

        Integer count = myUserMapper.getCount();
        pageInfo.setTotal(count);
        return pageInfo;
    }

    @Override
    public List<MyUser> getUserByKeyword(String keyword) {
        return null;
    }

    @Override
    public void addUser(MyUser user) {
        myUserMapper.insert(user);

    }

    @Override
    public void update(MyUser user) {
        myUserMapper.updateByPrimaryKey(user);

    }

    @Override
    public void deleteById(String id) {
        myUserMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void deleteByIdBatch(List<String> idList) {

        myUserMapper.deleteBatchRemove(idList);
    }

    @Override
    public List<Role> AssginByUserId (String userId) {

        List<Role> assginRoleList = myUserMapper.AssginByUserId(userId);

        return assginRoleList;

    }

    @Override
    public List<Role> noAssginByUserId(String userId) {

        List<Role> noAssginRoleList = myUserMapper.noAssginByUserId(userId);
        return noAssginRoleList;
    }

    @Override
    public void doAssgin(String userId, List<String> roleIdList) {
        UserRole userRole = new UserRole();
        userRole.setGmtCreate(new Date());
        userRole.setGmtModified(new Date());

        myUserMapper.insertUserAndRoleRelation(userId,roleIdList,userRole);
    }
        /**
         * 登录认证换取JWT令牌
         *
         * @param username 用户名
         * @param password 密码
         * @return 结果
        */
    @Override
    public String login(String username, String password) {

        Authentication authentication=null;

//        String encodePswd = passwordEncoder.encode(password);

        try{

            authentication=authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username,password));

        }catch (Exception e){

            throw new RuntimeException("用户名和密码错误"+e.getMessage()+e.getClass());

        }
        SecurityUser loginUser =(SecurityUser) authentication.getPrincipal();

        String token=jwtUtils.generateToken(loginUser.getUsername());

        return token;
    }

    @Override
    public MyUser login(String username) {
        MyUserExample example = new MyUserExample();
        MyUserExample.Criteria criteria = example.createCriteria();

        criteria.andUsernameEqualTo(username);

        List<MyUser> myUsers = myUserMapper.selectByExample(example);
        if (myUsers!=null && myUsers.size()>0){
            MyUser myUser = myUsers.get(0);

            return myUser;

        }
        return null;
    }
}
